package cn.taobaoke.restful.domain;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * AccountsRepository类. +-----------------------------------------------------------------------------------------------+
 * |类名称: AccountsRepository | |创建时间: 5/31/17 | |功能简介: | |参考链接: | +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
@Deprecated
@Transactional
public interface AccountsRepository extends JpaRepository<AccountsEntity, Long> {

    Optional<AccountsEntity> findByIdAndDomainId(Long id, Long domainId);

    @Query("from AccountsEntity acc where acc.agentId = :agentId and acc.domainId = :domainId order by acc.date desc")
    List<AccountsEntity> findByAgentIdOrderByDateDesc(@Param("agentId") Long agentId, @Param("domainId") Long domainId);

    @Query("from AccountsEntity acc where acc.agentId != null and acc.domainId = :domainId")
    Page<AccountsEntity> findAgentAccountsByPage(@Param("domainId") Long domainId, Pageable pageable);

    List<AccountsEntity> findByDomainIdAndAgentIdIn(Long domainId, Collection<Long> agentIds);

    @Query("update AccountsEntity acc set acc.isPayed = true where acc.date < :year_month and acc.domainId = :domainId")
    @Modifying
    void setPayedByDateLessThan(@Param("year_month") String date, @Param("domainId") Long domainId);
}
